/*
 * TBX: ToolBoX of Simgrid : some convenient tools (FIFO lists, hash
 * tables, graphs,...)  Copyright (C) 2002 "the MSG Team" (see AUTHORS
 * file).
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

#ifndef TBX_FIFO_H
#define TBX_FIFO_H

#include <stdlib.h>

/* Bucket structure */
typedef struct TBX_FIFO_Item {
  void *content;
  struct TBX_FIFO_Item *next;
  struct TBX_FIFO_Item *prev;
} *TBX_FIFO_Item_t;

/* FIFO table structure */
typedef struct TBX_FIFO {
  int count;
  TBX_FIFO_Item_t head;
  TBX_FIFO_Item_t tail;
} *TBX_FIFO_t;

/* help functions */
TBX_FIFO_Item_t TBX_FIFO_newItem(void);
void TBX_FIFO_freeItem(TBX_FIFO_Item_t);

/* API */
TBX_FIFO_t TBX_FIFO_newFIFO(void);
void TBX_FIFO_freeFIFO(TBX_FIFO_t);
void TBX_FIFO_insert(TBX_FIFO_t, void *);
void *TBX_FIFO_extract(TBX_FIFO_t);
void *TBX_FIFO_pop(TBX_FIFO_t);
TBX_FIFO_Item_t TBX_FIFO_getFirstItem(TBX_FIFO_t);

#define TBX_FIFO_getNextItem(i) ((i)?(i)->next:NULL)
#define TBX_FIFO_getPrevItem(i) ((i)?(i)->prev:NULL)
/* static __inline__ TBX_FIFO_Item_t TBX_FIFO_getNextItem(TBX_FIFO_Item_t i)  */
/* { */
/*   if(i) return i->next; */
/*   return NULL; */
/* } */
/* static __inline__ TBX_FIFO_Item_t TBX_FIFO_getPrevItem(TBX_FIFO_Item_t i) */
/* { */
/*   if(i) return i->prev; */
/*   return NULL; */
/* } */

void TBX_FIFO_remove(TBX_FIFO_t, void *);
void TBX_FIFO_removeItem(TBX_FIFO_t, TBX_FIFO_Item_t);

int TBX_FIFO_isInFIFO(TBX_FIFO_t, void *);

void **TBX_FIFO_FIFOToArray(TBX_FIFO_t);
TBX_FIFO_t TBX_FIFO_copy(TBX_FIFO_t);

#define TBX_FIFO_size(f) ((f)->count)
/* static __inline__ int TBX_FIFO_size(TBX_FIFO_t f) */
/* { */
/*   return f->count; */
/* } */

void *TBX_FIFO_findInFIFO(TBX_FIFO_t f, int (*test) (void *));

#define TBX_FIFO_foreach(f,i,n,type)                  \
   for(i=TBX_FIFO_getFirstItem(f);                    \
     ((i)?(n=(type)(i->content)):(NULL));             \
       i=TBX_FIFO_getNextItem(i))


#endif
